<?php

/**
* This is your child theme's functions.php file.
* Use this to write any custom functions you may need.
* You can use this to hook into the Hybrid theme (could also create a plugin to do the same).
*/

	remove_theme_support( 'hybrid-core-seo' );
	add_theme_support( 'post-formats', array( 'aside', 'status' ) );
	/* Constant paths. */
	define( BIRDBRAIN_DIR, get_stylesheet_directory() );
	define( BIRDBRAIN_DIR_URL, get_stylesheet_directory_uri() );

	define('FBML_NS_URI', 'http://www.facebook.com/2008/fbml');

	// define your api keys in a separate file
	require_once( BIRDBRAIN_DIR . '/api_keys.php' );
	$theme_controls = include_once( BIRDBRAIN_DIR . '/theme_controls.php' );
	if(empty($theme_controls)) {
		$social_networking_tools_enabled = 1;
	}
	$birdbrain_is_home = 0;
	$birdbrain_is_admin = 0;
	$birdbrain_is_single = 0;
	$birdbrain_is_singular = 0; 
	$birdbrain_is_page = 0; 
	$birdbrain_has_attachment = 0;
	function birdbrain_set_variables() {
		global $birdbrain_has_attachment;
		global $post;
		if ( is_home() ) {
			global $birdbrain_is_home; 
			$birdbrain_is_home = 1;
		}
		if ( is_admin() ) {
			global $birdbrain_is_admin;
			$birdbrain_is_admin = 1;
		}
		if ( is_single() ) {
			global $birdbrain_is_single;
			$birdbrain_is_single = 1;
		}
		if ( is_singular() ) {
			global $birdbrain_is_singular;
			$birdbrain_is_singular = 1; 
		}
		if ( is_page() ) {
			global $birdbrain_is_page;
			$birdbrain_is_page = 1;
		}
		$attachments = get_children("post_parent=$post->id&post_type=attachment&post_mime_type=image");
		if ( !empty($attachments) ) 
			$birdbrain_has_attachment = 1;
	}
	/*
	* Make sure to localize all your text if you plan on releasing this publicly
	*
	* @reference http://codex.wordpress.org/Translating_WordPress
	*/
	load_theme_textdomain('skeleton', get_stylesheet_directory());

	/*
	* Add actions
	*
	* @reference http://themehybrid.com/themes/hybrid/hooks
	* @reference http://codex.wordpress.org/Plugin_API/Action_Reference
	*/ 
	// add_action('action_hook_name','custom_function_name');
	add_action('wp_head', 'birdbrain_set_variables', 1);
//	add_action( 'hybrid_after_primary_menu', 'get_search_form' );
	add_action('wp_head', 'birdbrain_remove_actions', 1);
	add_action('wp_head', 'birdbrain_add_conditional_actions');
	add_action('hybrid_before_header', 'hybrid_get_primary_menu', 9);
	add_action('hybrid_header', 'hook_widgets_header', 11);
	add_action('hybrid_after_single', 'social_bookmarks');
	//add_action('hybrid_before_entry', 'social_bookmarks_toparea');

	/*
	* Add filters
	*
	* @reference http://themehybrid.com/themes/hybrid/filters
	* @reference http://codex.wordpress.org/Plugin_API/Filter_Reference
	*/
	// add_filter('filter_hook_name', 'custom_function_name');
	add_filter('hybrid_doctype','birdbrain_doctype');
	add_filter( 'hybrid_byline', 'birdbrain_byline' );

	if (!is_admin())
		add_filter('widget_text', 'do_shortcode', 11);

function post_formats_init() {
	add_post_type_support( 'post', 'post-formats' );
}

add_action('init', 'post_formats_init');


function hook_widgets_header() {
    if ( !is_active_sidebar( 'hook-header' ) )
        return;

    echo '<div id="utility-header" class="utility utility-header">';
    
        dynamic_sidebar( 'hook-header' );
    echo '</div>';
}

function birdbrain_add_conditional_actions() {
	global $birdbrain_is_home;
	global $birdbrain_is_single;
	global $birdbrain_is_singular;
	global $social_networking_tools_enabled;
	if (!$birdbrain_is_home ) {
		add_action('hybrid_after_header', 'hybrid_breadcrumb');
	}
	if ($birdbrain_is_singular && $social_networking_tools_enabled) {
		add_action('hybrid_after_entry', 'soc_links_end_of_entry', 12);
	}
	if ($birdbrain_is_home) {
		add_action('hybrid_after_entry', 'add_reply_link_to_status');
	}
}

function add_reply_link_to_status () {
	global $post;
	$post_id = $post->ID;
	$post_format = get_post_format( $post_id );
	if($post_format == 'status' || $post_format == 'aside') {
		echo post_reply_link( array( 'reply_text' => 'Reply  &rarr;'), get_the_id() );
	}
}

function birdbrain_remove_actions() {
	remove_action( 'hybrid_after_header', 'hybrid_page_nav' );
	remove_action( 'hybrid_after_header', 'hybrid_get_primary_menu');
//	remove_action( 'hybrid_after_entry', 'hybrid_entry_meta');
	remove_action( 'hybrid_before_content', 'hybrid_breadcrumb' );	
	//REVIEW
	if ( !is_page('leave-a-testimonial') ) {
		remove_action('wp_head','tdomf_stylesheet');
	}
	if ( !is_singular(array('post') ) && !(is_user_logged_in() && current_user_can( 'level_10' )) ) {
		remove_action( 'hybrid_before_entry', 'hybrid_byline');
	}
}

function social_bookmarks_toparea(){
	global $birdbrain_is_single;	
	if ($birdbrain_is_single) {
?>
	<div id = "topbookmarkarea">
		<div id="topsoclinkbox">
			<ul class="topsocmark">
				<li class="topsoclink">
					<a target="_blank" href="http://www.feedburner.com/fb/a/emailFlare?itemTitle='<?php the_title();?>'&amp;uri='<?php the_permalink(); ?>'">Email</a>
				</li>
				<?php if(function_exists('wp_print')) { ?>
				<li class="topsoclink">
					<?php print_link(); ?>
				</li>
				<?php } ?>
				<li class="topsoclink">
					<a href="<?php the_permalink(); ?>#bookmarkarea">Share</a>
				</li>
			</ul>
		</div>
	</div>
<?php
	}
}

function social_bookmarks() {
?>
	<div id ="bookmarkarea">
		<?php  if(function_exists('the_ratings')) { ?>
		<div id="ratingsbox">
			<ul>
				<li class="ratethis">
					<span>Rate This Article: </span>
				</li>
				<li class="rateimg">
					<?php the_ratings(); ?>
				</li>
			</ul>
		</div>
		<?php }  ?>
		<div id="soclinkbox">
			<ul class="socmark">
				<li class="soclink">
					<a target="_blank" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>">Stumble It</a>
				</li>
				<li class="soclink">
					<a target="_blank" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>">Facebook</a>
				</li>
				<li class="soclink">
					<a target="_blank" href="http://del.icio.us/post?url=<?php the_permalink() ?>&amp;title=<?php the_title(); ?>">Delicious</a>
				</li>
				<li class="soclink">
					<a target="_blank" href="http://www.feedburner.com/fb/a/emailFlare?itemTitle=<?php the_title();?>&amp;uri=<?php the_permalink(); ?>">Email</a>
				</li>
				<?php if(function_exists('wp_print')) { ?>
				<li class="soclink">
					<?php print_link(); ?>
				</li>
				<?php } ?>
				<li class="soclink go-top">
					<a href="<?php the_permalink(); ?>#body-container">Top</a>
				</li>
			</ul>
		</div>
	</div>
<?php
}

function birdbrain_byline($byline) {
	global $post;
	global $social_networking_tools_enabled;
	$post_format = get_post_format( $post_id );

	if ( (('post' == $post->post_type) && 'link_category' !== get_query_var( 'taxonomy' )) || (is_user_logged_in() && current_user_can( 'level_10' )) ) {
		$domain = hybrid_get_textdomain();
		//$posturl = apply_filters('sttop_blog_post_url', get_permalink($post->ID));
 		$data_text = $post->post_title;
		$posturl = get_permalink($post->ID);
		$href = urlencode($posturl);
		$layout = 'button_count';
		$show_faces = 'false';
		$width = 80;
		$height = 21;
		$action = 'like';
		$colorscheme = 'light';
		$number_of_comments = get_comments_number();
		$post_id = $post->ID;
		$testimonial = 0;
		foreach((get_the_category()) as $category) { 
			if ($category->cat_ID == 74) {
				$testimonial = 1;
			}
		} 
		if ( $testimonial == 1) {
			$author_url = get_post_meta($post_id, TDOMF_KEY_WEB, true);
			$author_name = get_post_meta($post_id, TDOMF_KEY_NAME, true);
			} else {
			$author_url = get_author_posts_url( get_the_author_meta( 'ID' ) );
			$author_name = get_the_author_meta( 'display_name' );
		}

		$fbtn = "<iframe src=\"http://www.facebook.com/plugins/like.php?app_id=157009494379776&amp;href=$href&amp;send=false&amp;layout=$layout&amp;width=$width&amp;show_faces=$show_faces&amp;action=$action&amp;colorscheme=$colorscheme&amp;font=lucida+grande&amp;height=$height\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:$width"."px; height:$height"."px;\" allowTransparency=\"true\"></iframe>";
		$plusone = "<g:plusone href='$posturl' size='medium'></g:plusone>";
		$tmbtn = "<a href='http://twitter.com/share' class='twitter-share-button' data-text='$data_text' data-url='$posturl' data-count='horizontal' data-via='free_thinker'>Tweet</a>";
		if ( $number_of_comments == 0 )
		        $comments_link = '<a class="comments-button" href="' . get_permalink() . '#respond" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . __( 'Leave a comment', $domain ) . '</a>';
		elseif ( $number_of_comments == 1 )
		        $comments_link = '<a class="comments-button" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . __( '1 Comment', $domain ) . '</a>';
		else
		        $comments_link = '<a class="comments-button" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( __( '%1$s Comments', $domain ), $number_of_comments ) . '</a>';
		$byline = '<div class="byline_div">';
		if ($social_networking_tools_enabled == 1 && !is_preview()) {
			$byline .= '<div class="byline_right"><ul>';
			$byline .= '<li class="comments-button">' . $comments_link . '</li>';
			$byline .= '<li class="googlebuzz">' . $plusone . '</li>';
			$byline .= '<li class="tweetmeme">' . $tmbtn . '</li>';
			$byline .= '<li class="fblike">' . $fbtn . '</li>';
			$byline .= '</ul></div>';
		} 
		$byline .= '<div class="byline">';
		$byline .= sprintf( __( '<span class="byline-prep byline-prep-author"></span> %1$s <span class="byline-prep byline-prep-published">|</span> %2$s', $domain ), '<span class="author vcard"><a class="url fn n" href="' . $author_url . '" title="' . $author_name . '">' . $author_name . '</a></span>', '<abbr class="published" title="' . sprintf( get_the_time( __( 'l, F jS, Y, g:i a', $domain ) ) ) . '">' . sprintf( get_the_time( get_option( 'date_format' ) ) ) . '</abbr>' );
		$byline .= '</div>';

		$byline .= '</div>';
		if(!is_home() && false === $post_format) {
			$byline .= '<div id="ad_below_byline">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3505281667688210";
/* Humbug Below Byline */
google_ad_slot = "1395081489";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>';
		}
  }
	return $byline;
}

function adsense_after_entry() {
	if(!is_home()) {
		$googleadsensecode = '<div id="ad_after_entry">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3505281667688210";
/* humbug after entry */
google_ad_slot = "4620523443";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>';
		echo $googleadsensecode;
	}
}

add_action('hybrid_after_entry', 'adsense_after_entry', 11);

function soc_links_end_of_entry() {
	global $post;
	global $birdbrain_is_single;
	$post_id = $post->ID;
	$post_format = get_post_format( $post_id );
	if( !is_preview() ) {
		if ( 'link_category' !== get_query_var( 'taxonomy' ) && $post_format != 'status' ) {
			//$data_url = apply_filters('stbottom_blog_post_url', get_permalink($post->ID));
			$data_text = $post->post_title;
			$data_url = get_permalink($post->ID);
			$href = urlencode($data_url);
			// Facebook Like Options: https://developers.facebook.com/docs/reference/plugins/like
			$layout = 'box_count';	//fblike
			$show_faces = 'false';	//fblike
			$width = 50;	//fblike
			$height = 25;	//fblike
			$action = 'like';	//fblike
			$colorscheme = 'light';	//fblike

			$fbtn = "<iframe src=\"http://www.facebook.com/plugins/like.php?app_id=157009494379776&amp;href=$href&amp;send=false&amp;layout=$layout&amp;width=$width&amp;show_faces=$show_faces&amp;action=$action&amp;colorscheme=$colorscheme&amp;font=lucida+grande&amp;height=$height\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:$width"."px;\" allowTransparency=\"true\"></iframe>";

			$plusone = "<g:plusone size='tall'></g:plusone>";
			$tmbtn = "<a href='http://twitter.com/share' class='twitter-share-button' data-text='$data_text' data-url='$data_url' data-count='vertical' data-via='free_thinker'>Tweet</a>";
			$sbtn = '<su:badge layout="5"></su:badge>';

			$soclinks = '<div class="soclink_div">';
			$soclinks .= '<div class="soclink_right"><ul>';
			$soclinks .= '<li class="tweetmeme">' . $tmbtn . '</li>';
			$soclinks .= '<li class="googlebuzz">' . $plusone . '</li>';
			$soclinks .= '<li class="stumbleupon">' . $sbtn . '</li>';
			$soclinks .= '<li class="fblike">' . $fbtn . '</li>';
			$soclinks .= '</ul><div style="clear: both;"></div></div>';
			$soclinks .= '</div>';
			echo $soclinks;
		} 
	}
}


function birdbrain_doctype() {
        if ( !preg_match( "/MSIE 6.0/", esc_attr( $_SERVER['HTTP_USER_AGENT'] ) ) )
                $doctype = '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
        $doctype .= '<!DOCTYPE html PUBLIC "-//humbug.in//dtd xhtml 1.1 plus target 1.0//EN" "http://humbug.in/wp-content/themes/birdbrain/dtd/xhtml11-target-iframe-gb-fbml.dtd">' . "\n";
        return $doctype;
}



/**
 * After each entry.
 * @since 0.5
 */
function hybrid_after_entry_div() {
    do_action( 'hybrid_after_entry_div' );
}


function custom_breadcrumb_args( $args = '') {
    $args['singular_aside_taxonomy'] = 'category';
    $args['separator'] = ' ';
    $args['before'] = '';
		$args['after'] = false;
		$args['front_page'] = true;
		$args['echo'] = true;
    return $args;
}
add_filter( 'breadcrumb_trail_args', 'custom_breadcrumb_args' );


function add_css_to_breadcrumb_trail_end ($breadcrumb) {
	$string = $breadcrumb;
	$patterns = array();
	$patterns[0] = '/^<div.*<\/a>/';
	$patterns[1] = '/<\/div>/';
	$replacements = array();
	$replacements[1] = '';
	$replacements[0] = '';
	$target = trim(preg_replace($patterns, $replacements, $string));
	$replacement = '<span class="trail-end">'.$target.'</span>';
	$str = str_replace($target, $replacement, $breadcrumb);
	return $str;
}

add_filter( 'breadcrumb_trail', 'add_css_to_breadcrumb_trail_end', 12);

function birdbrain_comment_form_args( $args ) {
	global $user_identity;

	$domain = hybrid_get_textdomain();
	$commenter = wp_get_current_commenter();
	$req = ( ( get_option( 'require_name_email' ) ) ? ' <span class="required">' . __( '*', $domain ) . '</span> ' : '' );

	$fields = array(
		'author' => '<p class="form-author"><label for="author">' . __( 'Name', $domain ) . $req . '</label> <input type="text" class="text-input" name="author" id="author" value="' . esc_attr( $commenter['comment_author'] ) . '" size="40" tabindex="1" /></p>',
		'email' => '<p class="form-email"><label for="email">' . __( 'Email', $domain ) . $req . '</label> <input type="text" class="text-input" name="email" id="email" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="40" tabindex="2" /></p>',
		'url' => '<p class="form-url"><label for="url">' . __( 'Website', $domain ) . '</label><input type="text" class="text-input" name="url" id="url" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="40" tabindex="3" /></p>'
	);

	$args = array(
		'fields' => apply_filters( 'comment_form_default_fields', $fields ),
		'comment_field' => '<p class="form-textarea"><textarea name="comment" id="comment" cols="60" rows="10" tabindex="4"></textarea></p>',
		'must_log_in' => '<p class="alert">' . sprintf( __( 'You must be <a href="%1$s" title="Log in">logged in</a> to post a comment.', $domain ), wp_login_url( get_permalink() ) ) . '</p><!-- .alert -->',
		'logged_in_as' => '<p class="log-in-out">' . sprintf( __( 'Logged in as <a href="%1$s" title="%2$s">%2$s</a>.', $domain ), admin_url( 'profile.php' ), $user_identity ) . ' <a href="' . wp_logout_url( get_permalink() ) . '" title="' . __( 'Log out of this account', $domain ) . '">' . __( 'Log out &raquo;', $domain ) . '</a></p><!-- .log-in-out -->',
		'comment_notes_before' => '',
		'comment_notes_after' => '',
		'id_form' => 'commentform',
		'id_submit' => 'submit',
		'title_reply' => __( 'Reply', $domain ),
		'title_reply_to' => __( 'Reply to %s', $domain ),
		'cancel_reply_link' => __( 'Click here to cancel reply.', $domain ),
		'label_submit' => __( 'Submit', $domain ),
	);

	return $args;
}

//add_filter ('comment_form_defaults', 'birdbrain_comment_form_args' , 11);




?>
